package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;

import dto.DataManager;

/**
 * Servlet implementation class Comment
 */
@WebServlet("/Comment")
public class Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  request.setCharacterEncoding("UTF-8");  
	       response.setContentType("text/html;charset=UTF-8");
	       PrintWriter out = response.getWriter();
	       DataManager dataManager = new DataManager();
	       HttpSession session = request.getSession();
	       User user = (String) session.getAttribute("username") == null ? null 
	    		   : dataManager.getUserWithUserName((String)session.getAttribute("username"));
	       int productId = request.getParameter("productId")==null? -1: Integer.parseInt(request.getParameter("productId"));
	       String comment = request.getParameter("comment");
	       String pageIndexStr = request.getParameter("pageIndex");
	       
	       System.out.println(pageIndexStr);
	       System.out.println(productId);
	      
	      model.Comment c = new model.Comment();
    	   c.setUserId(user == null ? -1: user.getUserId());
    	   c.setProductId(productId);
    	   //doan code cat cac doan xuong dong, them <br> sau mỗi lần xuống dòng
    	   String [] commentSplit = comment.split("\n");
    	  comment = "";
    	  for(int i = 0 ; i< commentSplit.length;i++)
    	  {
    		  comment+=commentSplit[i];
    		  comment+="<br>";
    	  }
    	   
    	   c.setUsercomment(comment);
	       if(user!=null && productId!= -1 && comment != null && dataManager.insertComent(c))
	       {
	    	   out.print("<div class=\"item-comment\">"+
	    			   "<div class=\"comment-name\">" +
	    	   		"<a href=\"#\">"+user.getUsername()+"</a><br></div>");
	    	   out.print("<div class=\"comment-content\"><p>"+c.getUsercomment()+"</p></div></div>");
	    	  // out.println("chen comment thanh cong");
	    	   //response.sendRedirect("productDetail.jsp?productId="+productId);
	    	   //System.out.println(userId);
	    	   
	       }
	       if(pageIndexStr != null && productId != -1)
	       {
	    	   int pageIndex = Integer.parseInt(pageIndexStr);
	    	   List<model.Comment> comments = dataManager.getAllCommentWithProductId(productId);
	    	   List<model.Comment> subComment = new ArrayList<model.Comment>() ;
	    	   if(pageIndex*5 +4 < comments.size())
	    	   {
	    		   subComment = comments.subList(pageIndex*5, pageIndex*5+5);
	    	   }
	    	   else
	    	   {
	    		   subComment = comments.subList(pageIndex*5, (pageIndex*5)+(comments.size()%5));
	    	   }
	    	   for(model.Comment sc : subComment)
	    	   {
	    		   out.print("<div class=\"item-comment\">" +
						"<div class=\"comment-name\">"+
							"<a href=\"#\">"+dataManager.getUserWithUserId(sc.getUserId()).getUsername()+"</a><br>"+
						"</div>"+
						"<div class=\"comment-content\">"+
							"<p>"+sc.getUsercomment()+"</p>"+
						"</div>"+
					"</div>");
	    	   }
	    	   int numberOfpage = comments.size()/5;
	    	   if(pageIndex == 0)
	    	   {
	    		   out.print("<div id= \"paging\">"+
	    				  
	   					"<button class=\"current_page\" onclick=\"paging("+(pageIndex)+","+productId+")\">"+(pageIndex+1)+"</button>");
	    		   int i = 1;
	    		   while(i<3&& i<=numberOfpage)
	    		   {
	    			  
	    				   out.print("<button class=\"relate_page\" onclick=\"paging("+(i)+","+productId+")\">"+(i+1)+"</button>");
	    				   i++;
	    		   }
	    		   if(pageIndex + 3 < numberOfpage)
	    			   out.print("...<button class=\"relate_page\" onclick=\"paging("+(pageIndex+1)+","+productId+")\">tiếp</button>");
	   					
	    	   }
	    	   else if(pageIndex > 0 )
	    	   {
	    		   
	    		   out.print("<div id= \"paging\">"+
		    				  
	   					"<button class=\"relate_page\" onclick=\"paging("+(pageIndex-1)+","+productId+")\">trước</button>...");
	    		   out.print("<button class=\"current_page\" onclick=\"paging("+(pageIndex)+","+productId+")\">"+(pageIndex+1)+"</button>");
	    		   int i = pageIndex+1;
	    		   while(i<(pageIndex+3)&& i<=numberOfpage)
	    		   {
	    			  
	    				   out.print("<button class=\"relate_page\" onclick=\"paging("+(i)+","+productId+")\">"+(i+1)+"</button>");
	    				   i++;
	    		   }

	    		   if(pageIndex + 3 < numberOfpage)
	    			   out.print("...<button class=\"relate_page\" onclick=\"paging("+(pageIndex+1)+","+productId+")\">tiếp</button>");
	    	   }
	    	   
	    	   
	    	   /*out.print("<div id= \"paging\">"+					
					"<button class=\"current_page\" onclick=\"paging(0,"+productId+")\">1</button>"+
					"<button class=\"relate_page\" onclick=\"paging(1,"+productId+")\">2</button>"+
					"<button class=\"relate_page\" onclick=\"paging(2,"+productId+")\">3</button>"+
					"... <button>tiếp</button>");*/
	       }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
